package cn.edu.nju.ws.aquila.rdf.canonicalization.comparator;

import java.util.Comparator;

import com.hp.hpl.jena.rdf.model.Literal;

/**
 * @author Gong Cheng
 * 
 */
public class LiteralComparator implements Comparator<Literal> {
	private Comparator<String> stringComparator = null;

	private LiteralComparator() {
		/* Nothing */
	}

	public LiteralComparator(Comparator<String> stringComparator) {
		this.stringComparator = stringComparator;
	}

	public int compare(Literal l1, Literal l2) {
		int result = 0;

		/* Datatype */
		result = this.stringComparator.compare(l1.getDatatypeURI(), l2
				.getDatatypeURI());
		if (result != 0) {
			return result;
		}

		/* Lexical form */
		result = this.stringComparator.compare(l1.getLexicalForm(), l2
				.getLexicalForm());
		if (result != 0) {
			return result;
		}

		/* Language */
		result = this.stringComparator.compare(l1.getLanguage(), l2
				.getLanguage());
		if (result != 0) {
			return result;
		}

		return result;
	}

	public void close() {
		this.stringComparator = null;
	}
}